package math2;

import inf.math.LinAlg;

/**
 * 
 * @author hbui
 * 
 */
class ScalarMultFunctionRnToR extends FunctionRnToR {

	private double alpha;
	private FunctionRnToR u;

	public ScalarMultFunctionRnToR(double alpha, FunctionRnToR u) {
		this.alpha = alpha;
		this.u = u;
	}

	@Override
	public double valueAt(double... x) {
		return alpha * u.valueAt(x);
	}

	@Override
	public double[] gradientAt(double... x) {
		double[] y = new double[x.length];
		LinAlg.scale(x.length, alpha, u.gradientAt(x), y);
		return y;
	}

	@Override
	public int getP() {
		if (this.alpha == 0)
			return 0;
		else
			return this.u.getP();
	}
}
